import Vue from 'vue';
import App from './App.vue';
import router from './router';
import axios from 'axios';

// 引入ElementUI组件库
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

// 请求拦截器,对每个请求都自动添加上token
axios.interceptors.request.use(config =>{
  if(!config.headers.Authorization && localStorage.getItem('userToken')){
    config.headers.Authorization = "Bearer " + localStorage.getItem('userToken')
  }

  // 拦截后需要放行
  return config;
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // console.log(to);
  if(to.path !=="/login" && !localStorage.getItem('userToken')){
    router.push('/login')
  }else{
    next();
  }

  next();
})

Vue.prototype.$axios = axios;

axios.defaults.baseURL = 'http://127.0.0.1:3000'

Vue.config.productionTip = false

new Vue({
  router,
  render: function (h) { return h(App) }
}).$mount('#app')
